package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.TextViewAdapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityMsgOtherAmoebaInstalledDetailBinding
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.lee.common.network.net.ObserverState
import java.util.*

class MsgOtherAmoebaInstalledDetailActivity :
    BaseActivity<ActivityMsgOtherAmoebaInstalledDetailBinding>(), View.OnClickListener {

    val viewModel by viewModels<NoticeViewModel>()

    private var adapter: TextViewAdapter? = null
    private var amibaInstallId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("其他阿米巴安装申请")

        setContentView(R.layout.activity_msg_other_amoeba_installed_detail)
    }

    override fun initVew() {

        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
        val manager = LinearLayoutManager(mContext)
        manager.orientation = RecyclerView.VERTICAL

        bindingView.topContent.rlvTiContinuedPriceIncrease.layoutManager = manager
        bindingView.topContent.rlvTiContinuedPriceIncrease.adapter =
            TextViewAdapter(R.layout.item_text_view).also {
                adapter = it
            }
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonAgree.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 1)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> amibaInstallApproval(amibaInstallId, 3, noticeId)
            R.id.superButton_agree -> amibaInstallApproval(amibaInstallId, 2, noticeId)
        }
    }

    /**
     * app端通过id获取单个安装申请接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getNoticeDetailById(noticeId)
            .observe(this, object : ObserverState<AmibaInstallByIdBean>() {
                override fun onDataChange(data: AmibaInstallByIdBean) {

                    applyUserId = data.applyUserId
                    approvalUserId = data.approvalUserId
                    amibaInstallId = data.amibaInstallId
                    bindingView.topContent.tvTiChooseAmoeba.text = data.orgName
                    bindingView.topContent.tvTiUnit.text = data.customerName
                    bindingView.topContent.tvTiRelatedProject.text = data.projectName
                    bindingView.topContent.tvTiInstallDate.text = data.installDate
                    bindingView.topContent.tvTiEndDate.text = data.endDate
                    bindingView.topContent.tvTiInstallLocation.text = data.installPosition
                    bindingView.topContent.tvTiPreQuote.text = data.preMoney
                    bindingView.topContent.tvTiApplicant.text = data.createUser
                    bindingView.topContent.tvTiApplicantStatus.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.topContent.tvTiApproverStatus.text = "已申请"
                        bindingView.topContent.tvTiApproverStatus.setTextColor(
                            ContextCompat.getColor(this@MsgOtherAmoebaInstalledDetailActivity, R.color.black)
                        )
                    } else if (data.approvalState == 2) {
                        bindingView.topContent.tvTiApproverStatus.text = "已同意"
                        bindingView.topContent.tvTiApproverStatus.setTextColor(
                            ContextCompat.getColor(
                                this@MsgOtherAmoebaInstalledDetailActivity, R.color.xui_btn_green_normal_color
                            )
                        )
                    } else if (data.approvalState == 3) {
                        bindingView.topContent.tvTiApproverStatus.setText("已拒绝")
                        bindingView.topContent.tvTiApproverStatus.setTextColor(
                            ContextCompat.getColor(
                                this@MsgOtherAmoebaInstalledDetailActivity, R.color.xui_config_color_red
                            )
                        )
                    }
                    bindingView.topContent.tvTiCc.text = data.sendUser
                    val strings: MutableList<String> = ArrayList()
                    for (amibaInstallContinueds in data.amibaInstallContinueds) {
                        strings.add(amibaInstallContinueds.continuedMoney)
                    }
                    adapter?.setList(strings)
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

    /**
     * app端阿米巴安装申请审批接口
     */
    private fun amibaInstallApproval(
        amibaInstallId: Int, approvalState: Int,
        noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getAmibaInstallApproval(amibaInstallId,approvalState,noticeId).observe(this,object :ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                finishActivity()
                customDialog.dismiss()
                super.onDataChange(data)
            }
            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }


}